'use strict';

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    const map = nums.reduce((prev, cur, idx) => {
        if (prev[cur] !== undefined && cur + cur === target) {
            prev[cur] = [prev[cur], idx];
        } else {
            prev[cur] = idx;
        }
        return prev;
    }, {});
    for(const key of Object.keys(map)) {
        const other = target - key;
        if (map[other]) {
            if (Array.isArray(map[other])) {
                return map[other];
            }
            return [map[key], map[other]];
        }
    }
    return null;
};
